<template>
<div class="container">
  <div class="bg-banner">
    <div class="text-scroll">
      <transition-group tag="ul" name="scroll-up">
        <li v-for="(bltitem,bltindex) in bulletin" :key="bltindex" v-show="bltindex===count">
          <p class="content-wrap">
            <span>公告：</span><span>{{bltitem.content}}</span>
          </p>
        </li>
      </transition-group>
    </div>
  </div>
  <!-- <div class="intro-content">
    <p class="intro-title">2018 寻找最美瑜伽馆主</p>
    <ul class="intro-txt">
      <li>湖南卫视金庸纪实频道、现代女性频道、新瑜传媒联袂主办</li>
      <li>聚焦中国瑜伽精英、寻找懂得瑜伽的你</li>
      <li>开启品牌之门共赢经营之道，发现瑜伽之美</li>
      <li>百万创业扶持计划，主流媒体追踪报道，瑜伽明星重资打造</li>
    </ul>
  </div> -->
  <div class="count-wrap count-position">
    <div class="count-item">
      <router-link :to="{name:'keeperRank'}">
        <p class="count-num">{{allCount.apply_count}}</p>
        <p class="count-txt">参与人数</p>
      </router-link>
    </div>
    <div class="count-item">
      <p class="count-num">{{allCount.vote_count}}</p>
      <p class="count-txt">投票人次</p>
    </div>
    <div class="count-item">
      <p class="count-num">{{allCount.visit_count}}</p>
      <p class="count-txt">访问量</p>
    </div>
  </div>
  <div class="search-wrap">
    <div class="search-block">
      <!-- <select class="select-block">
        <option value="上海市">上海市</option>
        <option value="南京市">南京市</option>
      </select> -->
      <div class="search-content">
        <form action='' class="search-form">
          <i class="iconfont icon-search" @click="search"></i>
          <input placeholder="搜索参赛馆主编号或姓名" class="search-input clear-input" type="search" name="keyword" id="search" v-model="keyword" @keydown.enter="search" onsearch="search">
        </form>
      </div>
    </div>
    <router-link :to="{name:'keeperRule'}">
      <div class="present">
        <span><i class="iconfont icon-present"></i></span>
        <p class="sm-txt">活动奖品</p>
      </div>
    </router-link>
  </div>
  <div class="apply-wrap">
    <div class="deadline">
      <p class="deadline-txt">
        <span>活动截止日期：</span>
        <span class="active-txt">2018</span>
        <span>年</span>
        <span class="active-txt">08</span>
        <span>月</span>
        <span class="active-txt">28</span>
        <span>日</span>
        <span class="active-txt">23:59</span>
      </p>
    </div>
    <router-link :to="{name:'keeperApply'}" v-if="self==0">
      <button class="clear-btn gradient-btn large-btn">我要报名</button>
    </router-link>
    <router-link :to="{name:'keeperDetail'}" v-if="self==1">
      <button class="clear-btn gradient-btn large-btn">查看我的主页</button>
    </router-link>
  </div>
  <div class="keeper-wrap">
    <div class="keeper-content">
      <div class="keeper-block" v-for="(keeperitem,keeperindex) in keeperList" :key="keeperindex">
        <router-link :to="{name:'keeperDetail',params:{id:`${keeperitem.id}`}}">
          <div class="keeper-img" :style="{backgroundImage: `url(${keeperitem.pics[0]})`}">
            <div class="keeper-title">{{keeperitem.vote_count}}票</div>
            <div class="keeper-info">
              <p class="info-id overflow-txt">编号：{{keeperitem.id}}</p>
              <div class="info-bottom">
                <span class="info-city overflow-txt">{{keeperitem.city}}</span>
                <span class="info-name overflow-txt">{{keeperitem.nickname}}</span>
              </div>
            </div>
          </div>
        </router-link>
        <button class="clear-btn gradient-btn middle-btn" @click="vote(keeperitem.id)">投一票</button>
      </div>
    </div>
  </div>
  <transition name="fade">
    <div class="pop-wrap" v-show="popShow" transition="fade" @click="hidePop">
      <div class="pop-content">
        <img class="success-img" src="http://piccdn.50yoga.cn/pics/public/sweet/images/shopkeeper/success.png" width="80" height="80">
        <p class="pop-title">投票成功</p>
        <p>感谢您的支持～</p>
        <button class="clear-btn gradient-btn vote-btn" @click="hidePop">确定</button>
      </div>
    </div>
  </transition>
  <v-tabbar></v-tabbar>
</div>  
</template>
<script>
import { get, post } from '../../common/request'
import tabbar from '@/components/shopkeeper/tabbar.vue'

export default {
  name: 'shopkeeper',
  data () {
    return {
      bulletin: [
        {
          content: '本次活动大奖价值最高10万元！'
        },
        {
          content: '最美馆主评选活动首发站常州！'
        }
      ],
      keeperList: [],
      allCount: {},
      count: 0,
      intervalId: null,
      playTime: 2000,
      keyword: '',
      self: 1,
      popShow: false
    }
  },
  components: {
    'v-tabbar': tabbar
  },
  methods: {
    hidePop () {
      this.popShow = false
    },
    getText () {
      let len = this.bulletin.length
      if (len === 0) {
        return
      }
      if (this.count === len - 1) {
        this.count = 0
      } else {
        this.count++
      }
    },
    search (e) {
      e.preventDefault()
      get(`/api/shield/vote/index`, {
        openid: localStorage.openid,
        keyword: this.keyword
      }).then(response => {
        this.keeperList = response.data
      })
    },
    vote (id) {
      post(`/api/shield/vote/vote`, {
        openid: localStorage.openid,
        obj_id: id
      })
      .then(response => {
        console.log(response)
        if (response.status === 409) {
          alert(response.message)
        } else {
          this.popShow = true
        }
      })
      .catch(error => {
        alert(error.message)
      })
    }
  },
  created  () {
    let openid = localStorage.openid
    if (!openid) {
      window.location.href = 'https://xcx2018.50yoga.cn/api/shield/vote/index'
      // window.location.href = 'http://192.168.0.119:2018/api/shield/vote/index'
    }
    get(`/api/shield/vote/show`, {
      openid: localStorage.openid
    })
    .then(response => {
      if (response.status === 404) {
        this.self = 0
      } else {
        this.self = 1
      }
    })
    get(`/api/shield/vote/index`, {
      openid: localStorage.openid
    }).then(response => {
      this.keeperList = response.data
      // this.wxShare()
    })
    get(`/api/shield/vote/st`, { openid: localStorage.openid }).then(response => {
      this.allCount = response.data
    })
    this.intervalId = setInterval(() => {
      this.getText()
    }, this.playTime)
  },
  destroyed () {
    clearInterval(this.intervalId) // 清除定时器
  }
}
</script>
<style scoped lang="stylus">
$keeperColor = #f06774;

.container {
  position: relative;
}

.bg-banner {
  width: 100%;
  height: 24.25rem;
  background: url('http://piccdn.50yoga.cn/pics/public/sweet/images/shopkeeper/banner.png') no-repeat;
  background-size: contain;

  .text-scroll {
    background-color: $keeperColor;
    width: 100%;

    & > ul {
      position: relative;
      width: 100%;
      height: 2.1875rem;
      overflow: hidden;

      & > li {
        width: 100%;
        height: 2.1875rem;
        line-height: 2.1875rem;
        overflow: hidden;
        position: absolute;

        .content-wrap {
          text-align: center;
          color: #fff696;
          font-size: 0.9375rem;

          & > span {
            font-size: 0.9375rem;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
        }
      }
    }
  }
}
.intro-content{
  width 100%
  padding 0.9375rem;
  box-sizing border-box;
  line-height 1.8;
  .intro-title{
    font-size 1rem;
    font-weight bold;
  }
  .intro-txt{
    font-size 0.875rem;
  }
}
// 公告跑马灯动画
.scroll-up-enter-active, .scroll-up-leave-active {
  transition: all 0.5s ease;
}

.scroll-up-enter {
  transform: translate3d(0, 100%, 0);
}

.scroll-up-leave-to {
  transform: translate3d(0, -100%, 0);
}

.search-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  width:92%;
  margin:0 auto;

  .search-block {
    display: flex;
    height: 1.875rem;
    border: 1px solid #8a8a8a;
    border-radius: 0.3125rem;
    box-sizing: border-box;
    flex:1;
    justify-content:center;

    .search-content {
      // border-left 1px solid #8a8a8a
      width: 14.375rem;
      display: flex;
      justify-content: center;

      .search-form {
        display: flex;
        justify-content: center;
        width: 70%;
      }

      .icon-search {
        color: #cfcfcf;
        margin-right: 0.2rem;
      }

      .search-input {
        width: 100%;
      }

      .search-input::-webkit-input-placeholder {
        font-size: 0.75rem;
        color: #cfcfcf;
      }
    }
  }

  .present {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-left: 0.9375rem;

    .icon-present {
      color: $keeperColor;
      font-size: 1.5rem;
    }
  }

  .sm-txt {
    font-size: 0.6875rem;
    line-height: 1.3;
    color: #161415;
  }
}

.count-position {
  margin: 1.875rem 0 2.5rem 0;
}

.apply-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 1.875rem 0 2.5rem 0;

  .deadline {
    font-size: 0.6875rem;
    color: #161415;

    .active-txt {
      color: $keeperColor;
    }
  }

  .large-btn {
    margin-top: 0.9375rem;
    width: 14.375rem;
  }
}

.keeper-wrap {
  width: 100%;
  padding-bottom: 5rem;

  .keeper-content {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    width: 96%;
    margin: 0 auto;

    .keeper-block {
      display: flex;
      flex-direction: column;
      margin: 0 2.08% 0.625rem 2.08%;
      width: 45.83%;

      .keeper-img {
        position: relative;
        width: 100%;
        height: 12.8125rem;
        border-radius: 0.3125rem;
        background-color: #f2f2f2;
        margin-bottom: 0.3125rem;
        background-size: cover;

        .keeper-title {
          position:absolute;
          min-width: 2.125rem;
          padding: 0 0.3rem;
          height: 1.5625rem;
          line-height: 1.5625rem;
          border-radius: 0 0 0.3125rem 0.3125rem;
          background: $keeperColor;
          color: #fff;
          text-align: center;
          margin-left: 0.625rem;
        }

        .keeper-info {
          position: absolute;
          width: 100%;
          line-height 1.5
          bottom: 0;
          font-size: 0.75rem;
          color: #fff;
          background: linear-gradient(rgba(255, 255, 255, 0), rgba(0, 0, 0, 0.8));
          border-radius: 0 0 0.3125rem 0.3125rem;
          display: flex;
          flex-direction: column;
          justify-content: center;

          .info-city {
            padding: 0 0.625rem;
            max-width:2.7rem
          }

          .info-name {
            font-size: 0.9375rem;
            flex 1
            margin-right 0.625rem;
          }

          .info-id {
            padding-left 0.625rem;
          }
          .info-bottom{
            display:flex;
            align-items: center;
          }
        }
      }
    }
  }
}
</style>

